<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>

    </style>
</head>
<body>
<div id="app">
<!--    页头组件-->
    <el-page-header @back="goBack" style="background-color: #0095d7;color: white;line-height: 60px" content="添加轮播图">

    </el-page-header>

    <el-divider></el-divider>
    <el-card>
        <!--添加上传文件的组件-->
        <!--***************************************-->
        <!--action代表提交地址  name代表文件上传时的参数名称-->
        <el-upload
                action="/upload"
                :limit="1"
                name="picFile"
                list-type="picture-card"
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :on-success="handleSuccess"
        >
            <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        <!--***************************************-->
    </el-card>
    <el-button @click="insert()">发布轮播图</el-button>
</div>
</body>
<!-- import Vue before Element -->
<!--引入Vue框架-->
<script src="https://unpkg.com/vue@2.6.14/dist/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<!--引入Axios框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>

<script>
   let v = new Vue({
        el: '#app',
        data: function() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                url:""
            }
        },
       methods:{
            goBack(){
                //返回上一页面
                history.back();
            },
           handleRemove(file, fileList) {
               console.log(file, fileList);
               let fileName=file.response;
               console.log("文件名："+fileName);
               axios.get("/remove?filename="+fileName).then(function () {
                   console.log("文件删除成功！")
               })
               v.url="";
           },
           handlePictureCardPreview(file) {
               this.dialogImageUrl = file.url;
               this.dialogVisible = true;
           },insert(){
                if(v.url==""){
                    v.$message.error("请选择上传的图片")
                    return;
                }
                    //发出异步请求
               axios.get("/banner/insert?url="+v.url).then(function () {
                   location.href="/admin.html"
               })
           },
           handleSuccess(response,file,fileList){
                console.log("文件上传")
               console.log("response="+response)
               console.log("file="+file)
               v.url=response
           }
       }
    })
</script>
</html>